<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\service\TaskMemberService;
use Webman\Stomp\Client;

class TaskMemberClockConsumer extends BaseConsumer
{
    // 要消费的队列名
    public $queue = 'task_member_clock';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 待办任务提醒
     * @param $data
     * @throws \Throwable
     */
    public function doConsume($data)
    {
        try {
            $taskMemberService = TaskMemberService::getInstance();
            $taskMemberCheckClock = $taskMemberService->checkMemberClock($data['task_id']);
            if (in_array($data['clock_time'], $taskMemberCheckClock['clock_time'])) {
                // 核对当前要消费的提醒时间是否还存在数据库中，如果存在就立即发送通知
                foreach ($taskMemberCheckClock['clock_users'] as $user) {
                    $data['receiver'] = $user;
                    $data['param']['send_at'] = date('Y-m-d H:i:s', time());
                    Client::send("send_notify_message", build_queue_data_with_xu($data));
                }
            }
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
